<?php
namespace Admin\Controller;


class BannerController extends AdminController 
{
	private $_banner = null;
	//初始化
	public function _initialize()
	{
		parent::_initialize();
		
		$this->_banner = D('banner');

	}

	public function index()
	{
		$search = I('get.name');
		if(empty($search)) {
			//分页查询
			$count = $this->_banner->count();// 查询满足要求的总记录数
			// var_dump($count);exit;
			$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(3)
			$show = $Page->show();// 分页显示输出

			$list = $this->_banner->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			// var_dump($list);exit;
			$this->assign('list',$list);
			$this->assign('page',$show);
			$this->display();
			exit;
		}
		// $banner = M('banner');
		//分页查询
		$count = $this->_banner->where('name like "%'.$search.'%"')->count();// 查询满足要求的总记录数
		// var_dump($count);exit;
		$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(3)
		$map['name'] = $_GET['name'];
		$Page->parameter=$map;

		$show = $Page->show();// 分页显示输出

		$list = $this->_banner->order('id desc')->where('name like "%'.$search.'%"')->limit($Page->firstRow.','.$Page->listRows)->select();
		// var_dump($list);exit;
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}

	public function add()
	{
		$this->display();
	}

	//执行添加操作
	public function doadd()
	{
			// var_dump($this->_banner->create());exit;

		// var_dump($_POST);exit;
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Uploads/'; // 设置附件上传根目录
		$upload->savePath = 'lunbo/'; // 设置附件上传（子）目录
		$upload->autoSub = true;
		$upload->subName = array('date','Ymd');
		$upload->saveName = time().mt_rand();
		// 上传文件
		$info = $upload->upload();
		// var_dump($info);exit;
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功
			$model = D('banner');
			// 取得成功上传的文件信息
			// $info = $upload->upload();
			// var_dump($info);exit;
			// 保存当前数据对象
			$data['imgurl'] = date('Ymd').'/'.$info['imgurl']['savename'];
			// $data['addtime'] = NOW_TIME;
			$data['name'] = $_POST['name'];
			$data['imglink'] = $_POST['imglink'];
			$data['status'] = $_POST['status'];
			// var_dump($data);
			// $model->create($data);
			// var_dump($model->create($data));exit;
			if(!$model->create($data)){
				$this->error($model->geterror());
			}else{
				$this->_banner->add();
				$this->success('上传成功',U('banner/index'));
			}
			// var_dump($data);exit;
			
		}
	}

	//执行编辑页面
	public function edit()
	{	$id =  I('get.id');
		// var_dump($id);
		$banner = M('banner');
		$list = $banner->where('id='.$id)->find();
		// var_dump($list);exit;
		$this->assign('list',$list);
		$this->display();
	}
	//执行修改操作
	public function save()
	{
		// var_dump($this->_banner->create());exit;
		//初始化
		if(!$this->_banner->create()){
			$this->error($this->_banner->getError());
			exit;
		}
		// var_dump($this->_banner->fetchSql()->save());exit;
		//执行修改 
		if($this->_banner->save() >= 0){
			$this->success("修改成功！",U('Banner/index'));
		}else{
			$this->error("修改失败");
		}
	}

	//执行删除操作
	public function del()
	{
		$id = I('get.id');
		// var_dump($id);
		$data = $this->_banner->where(array('id'=>array('eq',$id)))->find();
		// var_dump($data);
		// exit;
		$img_url = './Uploads/lunbo/'.$data['imgurl'];
		// var_dump($img_url);
		// exit;
		unlink($img_url);
		// var_dump($this->_banner->where('id='.$id)->delete());exit;
		if($this->_banner->where('id='.$id)->delete()>0){
			$this->success("删除成功！",U('Banner/index'));
		}else{
			$this->error("删除失败");
		}
	}



	//状态修改操作
	public function doajax()
	{
		$id=I('post.id');
		$status = $this->_banner->where(array('id'=>array('eq',$id)))->find();
		if($status['status']==0) {
			$status['status'] = 1;
			$list = $this->_banner->where(array('id'=>array('eq',$id)))->save($status);
			echo $str = '未营业';
		} else {
			$status['status'] = 0;
		$list = $this->_banner->where(array('id'=>array('eq',$id)))->save($status);

			echo $str = '营业中';
		}

		// // echo $str;
		// $list = $this->_banner->where(array('id'=>array('eq',$id)))->save($status);
		// // echo $list;
		// if($list>0) {
		// 	echo $str;
		// } else {
		// 	echo $list;
		// }

		// // $this->ajaxReturn($status);
	
	}
}
